/**
 * 文档作者: wj
 * 创建时间：2018年05月21日
 * 修改时间：2018年05月21日
 * 描述信息：添加收获地址界面
 */
import React, {Component} from 'react';
import './address-cu.scss';
import util from '../../util/util';
import userModel from '../api/user.model';
import windowService from '../api/window.service';

class AddressAdd extends Component {
    constructor(props) {
        super(props);
        this.wService = windowService;
        this.state = {
            data: this.wService.getAddressNode() && window.location.search.indexOf('?add') === -1 ? this.wService.getAddressNode() : undefined
        }
    }

    render() {
        document.body.style.background = '#f6f5f6';
        const data = this.state && this.state.data;
        return <div className='address-add-page top-center'>
            <div className='content'>
                <div>
                    <label>省/市</label><input 
                        ref='province' 
                        value={data && data.province} 
                        className='inp' type='text'
                        onChange={this.handleChange.bind(this, 'province')} />
                </div>
                <div>
                    <label>市/区/县</label><input ref='city'
                        value={data && data.city}
                        className='inp' 
                        type='text'
                        onChange={this.handleChange.bind(this, 'city')} />
                </div>
                <div className='street'>
                    <label>路牌建筑</label><textarea 
                        value={data && data.address}
                        ref='address'
                        onChange={this.handleChange.bind(this, 'address')}></textarea>
                </div>
                <div>
                    <label>联系人</label><input className='inp' 
                        value={data && data.contact} 
                        ref='contacts' 
                        type='text'
                        onChange={this.handleChange.bind(this, 'contact')} />
                </div>
                <div>
                    <label>手机号</label><input 
                        className='inp' 
                        value={data && data.contact_cellno} 
                        ref='phone' type='number'
                        onChange={this.handleChange.bind(this, 'contact_cellno')} />
                </div>
            </div>
           
            <button onClick={this.onSubmit.bind(this)} className='btn grean'>确定</button>
        </div>
    }
    /**
     * 提交表单
     */
    onSubmit() {
        const self = this,
            refs = this.refs,
            province = refs.province.value,
            city = refs.city.value,    
            address = refs.address.value,  
            contacts = refs.contacts.value,
            phone = refs.phone.value;
        let info = '';
        if (province === '') {
            info = '请填写省/市';
        } else if (city === '') {
            info = '请填写市/区/县';
        } else if (address === '') {
            info = '请填写路牌建筑';
        } else if (contacts === '') {
            info = '请填写联系人';
        } else {
            info = util.verification(phone);
        }
        if (info === '') {
            util.getApiData('post', '/svcShop/svcOpsAddress', {
                uid: userModel.svcRegWithWx.uid,
                apptoken: userModel.svcRegWithWx.apptoken,
                ops_type: windowService.getAddressNode() ? 'update' : "add",
                "udata": windowService.getAddressNode() ? this.state.data : {
                      "province": province,
                      "city": city,
                      "address": address,
                      "contact": contacts,
                      "contact_cellno": phone
                }
            }).then(res => {
                if (res.data.res === 1) {
                    self.props.history.push(`/address${util.getQueryStringByName('sku')? `?sku=${util.getQueryStringByName('sku')}` : ''}`);
                }
            })
        } else {
            window.$message.next({info: 'error', content: info});
        }
    }

    /**
     * input值修改
     * @param {String} name 属性名
     * @param {Element} e input节点属性
     */
    handleChange(name, e) {
        if (windowService.getAddressNode()) {
            const obj = Object.assign({}, this.state.data);
            obj[name] = e.target.value;
            this.setState({
                data: obj
            })
        }
    }
}

export default AddressAdd;
